package com.kiln.juxingqs.ui

import androidx.navigation.NavController
import androidx.navigation.findNavController
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.databinding.ActivityAvailableassetsBinding
import com.kiln.juxingqs.dialog.DateTimeDialog
import com.kiln.juxingqs.ui.vm.AvailableAssetsViewModel
import java.util.*


class AvailableAssetsActivity :
    BaseMiddleActivity<AvailableAssetsViewModel, ActivityAvailableassetsBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_availableassets
    }

    //    val adapter: AvailableAssetsFragmentPageAdapter by lazy {
//        AvailableAssetsFragmentPageAdapter(supportFragmentManager)
//    }
    val controller: NavController by lazy {
        findNavController(R.id.nav_host_fragment)
    }

    override fun initViewModel(): AvailableAssetsViewModel = createViewModel()

    override fun initView() {
        binding.event = this
        binding.vm = viewModel
        val calendar = Calendar.getInstance()
        val y = calendar.get(Calendar.YEAR)
        val m = calendar.get(Calendar.MONTH) + 1
        val d = calendar.get(Calendar.DAY_OF_MONTH)
        viewModel.endDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"
//        binding.viewPager.adapter = adapter
//        binding.navView.setupWithNavController(controller)
//        binding.tabLayout.setupWithViewPager(binding.viewPager)

//        when (intent.getIntExtra("index", 0)) {
//            0 -> {
//                binding.tabOne.isSelected = true
//            }
//            1 -> {
//                binding.viewPager.postDelayed({
//                    binding.viewPager.setCurrentItem(1, false)
//                }, 20)
//            }
//            2 -> {
//                binding.viewPager.postDelayed({
//                    binding.viewPager.setCurrentItem(2, false)
//                }, 20)
//            }
//        }

        binding.tabOne.isSelected = true
    }

    fun searchData() {
        when (viewModel.switchValue.value) {
            0, -1 -> {
                viewModel.pageOne.value = 0
                viewModel.holdOne()
            }
            1 -> {
                viewModel.pageTwo.value = 0
                viewModel.holdTwo()
            }
            2 -> {
                viewModel.pageThree.value = 0
                viewModel.holdThree()
            }
            3 -> {
                viewModel.pageFour.value = 0
                viewModel.holdFour()
            }
            4 -> {
                viewModel.pageFive.value = 0
                viewModel.holdFive()
            }
            5 -> {
                viewModel.pageSix.value = 0
                viewModel.holdSix()
            }
        }
    }

    override fun observerUI() {
        viewModel.switchValue.observe(this, {
            if (it >= 0) {
                when (it) {
                    5 -> {
                        controller.navigate(R.id.sixTab)
                        binding.tabOne.isSelected = false
                        binding.tabTwo.isSelected = false
                        binding.tabThree.isSelected = false
                        binding.tabFour.isSelected = false
                        binding.tabFive.isSelected = false
                        binding.tabSix.isSelected = true
                    }
                    4 -> {
                        controller.navigate(R.id.fiveTab)
                        binding.tabOne.isSelected = false
                        binding.tabTwo.isSelected = false
                        binding.tabThree.isSelected = false
                        binding.tabFour.isSelected = false
                        binding.tabFive.isSelected = true
                        binding.tabSix.isSelected = false

                    }
                    3 -> {
                        controller.navigate(R.id.fourTab)
                        binding.tabOne.isSelected = false
                        binding.tabTwo.isSelected = false
                        binding.tabThree.isSelected = false
                        binding.tabFour.isSelected = true
                        binding.tabFive.isSelected = false
                        binding.tabSix.isSelected = false

                    }
                    2 -> {
                        controller.navigate(R.id.threeTab)
                        binding.tabOne.isSelected = false
                        binding.tabTwo.isSelected = false
                        binding.tabThree.isSelected = true
                        binding.tabFour.isSelected = false
                        binding.tabFive.isSelected = false
                        binding.tabSix.isSelected = false

                    }
                    1 -> {
                        controller.navigate(R.id.twoTab)

                        binding.tabOne.isSelected = false
                        binding.tabTwo.isSelected = true
                        binding.tabThree.isSelected = false
                        binding.tabFour.isSelected = false
                        binding.tabFive.isSelected = false
                        binding.tabSix.isSelected = false
                    }
                    0 -> {
                        controller.navigate(R.id.oneTab)
                        binding.tabOne.isSelected = true
                        binding.tabTwo.isSelected = false
                        binding.tabThree.isSelected = false
                        binding.tabFour.isSelected = false
                        binding.tabFive.isSelected = false
                        binding.tabSix.isSelected = false
                    }
                }
            }
        })
    }

    fun changeTab(i: Int) {
        viewModel.switchValue.value = i
    }

    fun selectDate(index: Int) {
        DateTimeDialog(this@AvailableAssetsActivity).apply {
            ok = { y, m, d ->
                if (index == 0) {
                    viewModel.startDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"
//                    toast("${y}-${if (m < 10) "0${m}" else m}-${d}")
                } else {
                    viewModel.endDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"
                }
            }
        }.show()
    }

    fun current() {
        val calendar = Calendar.getInstance()
        val y = calendar.get(Calendar.YEAR)
        val m = calendar.get(Calendar.MONTH) + 1
        val d = calendar.get(Calendar.DAY_OF_MONTH)

        viewModel.endDate.value = "${y}-${if (m < 10) "0${m}" else m}-${d}"

        var mm = m - 2
        var yy = y
        if (mm <= 0) {
            mm = 12 - mm
            yy--
        }
        viewModel.startDate.value =
            "${yy}-${if (mm < 10) "0${mm}" else mm}-01"

        searchData()
    }
}